/**
 * 
 */
package com.base.myproject.client.busi.b2c;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseFileUploadDialog;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseImageCCRG;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;

/**
 * 每个项目都只有唯一的logo和banner
 * @author zgw
 *2009-8-6 下午07:29:49
 * javazgw@gmail.com
 */
public class Logo extends BaseJDBCPanel{
	static BaseStore<JDBCModelData> store = null;
	static BaseGrid grid = null;
	//业务代码
	final static String busi = "9001";
	public Logo() {
		super();
		init();
	
	}

	public void init() {

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5, 5, 0, 0));  
	this.add(initWeiget(),centerData);
		this.setBusino(busi);
		setdefault();
	}
	private void setdefault()
	{
		//store.addDefault("busicode", busi);
	}
	public ContentPanel initWeiget() {
		ContentPanel panel = new ContentPanel();
//		CreateGCodePanel();
		panel.add(CreateGCodePanel());
		return panel;
	}
	
	private BaseGrid CreateGCodePanel() {

		//CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
//		columns.add(cbs.getColumn());
		ColumnConfig logo = new ColumnConfig("logo", "logo", 100);
		
//		AdapterField bcb = new AdapterField(new Button("b"));
		BaseImageCCRG biccrg = new BaseImageCCRG(15){
			public Object render(final ModelData model, final String property,
					ColumnData config, final int rowIndex, final int colIndex,
					ListStore lstore, Grid grid) {
			//System.out.println("++" + rowIndex + " colIndex=" + colIndex);
			HorizontalPanel hp = new HorizontalPanel();
			Button uploadbutton = new Button("");
			Button lookimagebutton = new Button("");
			Button htmllinkbutton = new Button("");
			Button soundbutton = new Button("");
			uploadbutton.setToolTip("上传");
			uploadbutton.setIconStyle("uploadicon");
			lookimagebutton.setToolTip("看图");
			lookimagebutton.setIconStyle("imageicon");
			htmllinkbutton.setToolTip("超级连接");
			htmllinkbutton.setIconStyle("htmllinkicon");
			soundbutton.setToolTip("多媒体");
			soundbutton.setIconStyle("soundicon");
			if ((getMode() & BaseImageCCRG.UPLOADMODE) == BaseImageCCRG.UPLOADMODE) {
				hp.add(uploadbutton);
			}
			if ((getMode() & BaseImageCCRG.LOOKIMAGEMODE) == BaseImageCCRG.LOOKIMAGEMODE) {
				hp.add(lookimagebutton);
			}
			if ((getMode() & BaseImageCCRG.HTMLLINKMODE) == BaseImageCCRG.HTMLLINKMODE) {
				hp.add(htmllinkbutton);
			}
			if ((getMode() & BaseImageCCRG.SOUNDMODE) == BaseImageCCRG.SOUNDMODE) {
				hp.add(soundbutton);
			}
			uploadbutton.addSelectionListener(new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					//System.out.println(model.get(property));
					//System.out.println("------" + rowIndex + " colIndex="
						//	+ colIndex);

					BaseFileUploadDialog bf = com.base.myproject.client.ui.BaseFileUploadDialog.getInstance()
							;
					String imagepath = "logo";
					bf.setServletPath("gupld?zgw=uploadbutton&zgw3=2&imagepath="+imagepath+"&"+store.getStoreinfo().getStoreInfoString());
					bf.show();
				}

			});
			lookimagebutton.addSelectionListener(new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					//System.out.println(model.get(property));
					//System.out.println("------1" + rowIndex + " colIndex="
					//		+ colIndex);

					com.base.myproject.client.ui.BasePopupImage.getInstance().show(model.get(property).toString());
				}

			});
			htmllinkbutton.addSelectionListener(new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					//System.out.println(model.get(property));
					//System.out.println("------2" + rowIndex + " colIndex="
					//		+ colIndex);

				}

			});
			soundbutton.addSelectionListener(new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					//System.out.println(model.get(property));
					//System.out.println("------3" + rowIndex + " colIndex="
					//		+ colIndex);
					BaseFileUploadDialog bf = com.base.myproject.client.ui.BaseFileUploadDialog.getInstance()
					;
			bf.setServletPath("gupld?zgw=soundbutton&zgw3=soundbutton2");
			bf.show();
			
				}

			});
			// hp.add(new Button("d"));
			return hp;
			// return new Html("<a>html</a>");
		}};
		//TextField<String> logotext = new TextField<String>();
		logo.setEditor(biccrg);
		logo.setRenderer(biccrg);
		columns.add(logo);
		
		ColumnConfig banner = new ColumnConfig("banner", "banner", 100);
		TextField<String> bannertext = new TextField<String>();
		banner.setEditor(new CellEditor(bannertext));
		columns.add(banner);
		


		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("logo");
		type.addField("banner");
		


		StoreInfo info = new StoreInfo();

		info.setTablename("b2clogo");
		info.setSql("select * from b2clogo");
		info.setKeyname(new String[] { "logo" }); // 主键
		
//		info.setParent(store);// 关联主表
//		info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
//		info.setLinkparentcolumns(new String[] { "codename" }); // 关联主表字段
		 store = new BaseStore<JDBCModelData>(type, info);
		store.setName("b2clogo");
		ColumnModel cm = new ColumnModel(columns);
		BaseGrid contractgcodegrid = new BaseGrid(store, cm);
//
//		cbs.setSelectionMode(SelectionMode.SINGLE);
//
//		contractgcodegrid.setSelectionModel(cbs);
		contractgcodegrid.setAutoHeight(true);
		contractgcodegrid.setAutoWidth(true);

		setMaincomponent(contractgcodegrid);
		registerMainStore(store);
		registerStore(store);
		return contractgcodegrid;
	}
}
